package dk.degofedal.airfoil

import org.w3c.dom.DOMImplementation
import org.apache.batik.dom.GenericDOMImplementation
import org.apache.batik.svggen.SVGGraphics2D
import java.io.OutputStreamWriter
import java.io.FileWriter
import scala.Math.Pi
import scala.swing.SimpleGUIApplication
import scala.swing.MainFrame
import scala.swing.Button
import java.awt.Dimension
import java.awt.Graphics2D
import java.awt.Graphics
import javax.swing.JPanel
import java.awt.Color
import scala.swing.SimpleSwingApplication
import scala.swing.Panel

object Test extends SimpleSwingApplication {

	val canvasWidth = 1300
	val canvasHeight = 200

	val foil = {
		// instantiate foil
	    val testFoil = Naca00(15) 
	    
	    // generate points on the foil
	    var t : Double = -1.0
	    val dt = 0.002
	    
	    var airfoilPoints : List[(Int,Int)] = List()
	    
	    var scale = {v : Double => (1000 * v).toInt } 
	    while (t < 1) {
	      val P = testFoil.foil(t)
	      airfoilPoints = (scale(P._1), scale(P._2)) :: airfoilPoints
	      t += dt
	    }
	    
	    val refactor = (Pi * 66 * 300.0 / 25.4 ) / Tool.foilLength(airfoilPoints)
	    println("refactor: "+refactor)
	    scale = {v : Double => (refactor * v).toInt }
	    val refactored = airfoilPoints.map{ P : (Int,Int) => (scale(P._1), scale(P._2)) }
	    
	    //airfoilPoints.foreach{P : (Int, Int) => println(P._1 + ", "+P._2)}
	    
	    // find minimum y value to fix offset
	    val min_y = refactored.foldLeft(0){(min, P : (Int,Int)) => if(P._2 < min) P._2 else min}
	    
	    // implicit return
	    refactored.map{ P : (Int,Int) => (P._1, P._2-min_y) }
	}
    
    println("Foil-length " + Tool.foilLength(foil))
    
    
	
  object canvas extends JPanel {
    
	setPreferredSize(new Dimension(canvasWidth, canvasHeight))
	  
    override def paint(g:Graphics) {
      //g.drawLine(0, 0, canvasWidth, canvasHeight)
      foil.foldLeft(foil.head){
    	  (P0, P1) => {g.drawLine(P0._1 ,P0._2 ,P1._1 ,P1._2); P1}
      }
    }
    
  }
  
  def top = new MainFrame{
    title = "Test"
      
    contents = new Panel {
          background = Color.white
          preferredSize = new Dimension(canvasWidth, canvasHeight)
          focusable = true      
          peer.add(canvas)
          pack()
      }
    
  }
  
  override def main(args: Array[String]): Unit = { 
    
    super.main(args)

  /*def step(list : List[(Int,Int)], graphics : SVGGraphics2D) : Unit = {
    if(list != Nil && list.tail != Nil){
      val P0 = list.head
      val tail = list.tail
      val P1 = tail.head
      //println("(" + P0._1 + ", " + P0._2 + "), (" + P1._1 + ", " + P1._2 + ")")
      graphics.drawLine(P0._1 ,P0._2 ,P1._1 ,P1._2)
      step(tail,graphics)
    }
  }*/
  }
  
  def renderSVG(foil : List[(Int, Int)]): Unit = {
    // generate SVG
    // get DOM implementation
    val domImpl = GenericDOMImplementation.getDOMImplementation();
    
    // Create an instance of org.w3c.dom.Document.
    val svgNS = "http://www.w3.org/2000/svg";
    val document = domImpl.createDocument(svgNS, "svg", null);
    
    // Create an instance of the SVG Generator.
    val svgGenerator = new SVGGraphics2D(document);

    // render airfoil to svg-graphics
    //step(corrected, svgGenerator)
    foil.foldLeft(foil.head){
      (P0, P1) => {svgGenerator.drawLine(P0._1 ,P0._2 ,P1._1 ,P1._2); P1}
    }
    
    // Finally, stream out SVG to the standard output using
    // UTF-8 encoding.
    val useCSS = true; // we want to use CSS style attributes
    //val out = new OutputStreamWriter(System.out, "UTF-8");
    val out = new FileWriter("/tmp/test.svg");
    svgGenerator.stream(out, useCSS);
  }
}